package com.woniuxy.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.woniuxy.dao.BorrowDao;

import com.woniuxy.entity.Borrow;
import com.woniuxy.service.BorrowService;
@Service
public class BorrowServiceImpl implements BorrowService{

	@Autowired
	BorrowDao borrowDao;


	@Override
	public void borrowBookByBid(int bid, Date date, int flag, int uid) {
		borrowDao.borrowBookByBid(bid, date, flag, uid);
	
	}



	@Override
	public int selectBidCountByBid(int bid) {
		int count=borrowDao.selectBidCountByBid(bid);
		return count;
	}



	@Override
	public void updateBookCountByBid(int bid,int count) {
		borrowDao.updateBookCountByBid(bid,count);
		
	}



	@Override
	public int selectCountByBid(int bid) {
		int count=borrowDao.selectCountByBid(bid);
		return count;
	}



	@Override
	
	public String updateBookFlagByBid(int bid, int flag,int uid) {
		borrowDao.updateBookFlagByBid(bid, flag,uid);
		int count=borrowDao.selectCountByBid(bid)+1;
		updateBookCountByBid(bid, count);
		return "success!";
	}



	@Override
	public int selectFlagByBid(int bid,int flag) {
		int fflag=(int)borrowDao.selectFlagByBid(bid,flag);
		return fflag;
	}



	@Override
	public List<Borrow> selceAllBorrow() {
		List<Borrow>borrows=borrowDao.selceAllBorrow();
		return borrows;
	}



	



	@Override
	public String borrow(int bid, Date date, int flag, int uid) {
	
		/*borrowDao.borrowBookByBid(bid, date, flag, uid);
		int count=borrowDao.selectCountByBid(bid)-1;
		System.out.println(count+"总数");
		borrowDao.updateBookCountByBid(bid,count);
		System.out.println(borrowDao.selectCountByBid(bid));
		System.out.println(borrowDao.selectFlagByBid(bid,1));*/
		if (borrowDao.selectCountByBid(bid)>0&&selectFlagByBid(bid, 1)==0) {
			borrowDao.borrowBookByBid(bid, date, flag, uid);
			int count=borrowDao.selectCountByBid(bid)-1;
			borrowDao.updateBookCountByBid(bid,count);
			return "success!";
		}else {
			return "failure!";
		}
		
		
	}





	
}
